package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.JoinAbstractLambdaWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.interfaces.WrapperFunction;
import com.linkhub.oms.common.enums.product.ProductSearchTypeEnum;
import com.linkhub.oms.common.enums.product.SkuMatchStatusEnum;
import com.linkhub.oms.dao.mapper.ShopProductSkuEntityMapper;
import com.linkhub.oms.dao.mpservice.IShopProductSkuService;
import com.linkhub.oms.entity.LocalProductEntity;
import com.linkhub.oms.entity.ShopProductEntity;
import com.linkhub.oms.entity.ShopProductSkuEntity;
import com.linkhub.oms.entity.ShopProductSkuMappingEntity;
import com.linkhub.oms.entity.resulttype.ShopProductListResultEntity;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商店商品sku表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-20
 */
@Service
@Getter
public class ShopProductSkuServiceImpl extends MPJBaseServiceImpl<ShopProductSkuEntityMapper, ShopProductSkuEntity> implements IShopProductSkuService {


    @Resource
    private ShopProductSkuEntityMapper shopProductSkuEntityMapper;


    @Override
    public MPJLambdaWrapper<ShopProductSkuEntity> getShopProductListQueryWrapper(Integer matchStatus, Integer searchType, String keyword, List<String> sysShopIds) {

        MPJLambdaWrapper<ShopProductSkuEntity> lambdaWrapper = JoinWrappers.lambda(ShopProductSkuEntity.class);

        // 查询字段
        lambdaWrapper.selectAll(ShopProductSkuEntity.class)
                .selectAs(ShopProductEntity::getTitle, ShopProductListResultEntity::getProductTitle)
                .selectAs(ShopProductSkuMappingEntity::getSkuId, ShopProductListResultEntity::getMatchSkuId)
                .selectAs(LocalProductEntity::getSku, ShopProductListResultEntity::getMatchSku)
                .selectAs(LocalProductEntity::getImgUrl, ShopProductListResultEntity::getMatchProductImgUrl)
                // 商品表关联
                .innerJoin(ShopProductEntity.class, on ->
                        on.eq(ShopProductSkuEntity::getSysShopId, ShopProductEntity::getSysShopId)
                                .eq(ShopProductSkuEntity::getProductId, ShopProductEntity::getProductId));


        // 匹配关系 on条件
        WrapperFunction<JoinAbstractLambdaWrapper<ShopProductSkuEntity, ?>> mappingOnCondition = on ->
                on.eq(ShopProductSkuEntity::getSysShopId, ShopProductSkuMappingEntity::getSysShopId)
                        .eq(ShopProductSkuEntity::getSkuId, ShopProductSkuMappingEntity::getShopSkuId);
        // 已匹配
        if (SkuMatchStatusEnum.MATCH.getCode().equals(matchStatus)) {
            lambdaWrapper.innerJoin(ShopProductSkuMappingEntity.class, mappingOnCondition);
        } else {
            // 全部或未匹配
            lambdaWrapper.leftJoin(ShopProductSkuMappingEntity.class, mappingOnCondition);
        }

        // 关联获取本地产品信息
        lambdaWrapper
                .leftJoin(LocalProductEntity.class, on ->
                        on.eq(ShopProductSkuMappingEntity::getSkuId, LocalProductEntity::getSkuId));


        lambdaWrapper.in(ShopProductSkuEntity::getSysShopId, sysShopIds);
        // 未匹配
        if (SkuMatchStatusEnum.UN_MATCH.getCode().equals(matchStatus)) {
            lambdaWrapper.isNull(ShopProductSkuMappingEntity::getSkuId);
        }

        // 关键词条件
        if (StringUtils.isNotBlank(keyword)) {
            lambdaWrapper.like(ProductSearchTypeEnum.PRODUCT_TITLE.getCode().equals(searchType),
                    ShopProductEntity::getTitle, keyword);
            lambdaWrapper.like(ProductSearchTypeEnum.PRODUCT_SKU.getCode().equals(searchType),
                    ShopProductSkuEntity::getSku, keyword);
        }

        lambdaWrapper.orderByDesc(ShopProductSkuEntity::getUpdateTime);
        return lambdaWrapper;
    }

    @Override
    public void deleteShopProductSkus(String sysShopId, List<Long> productIds) {
        if (CollUtil.isEmpty(productIds)) {
            return;
        }
        super.lambdaUpdate()
                .eq(ShopProductSkuEntity::getSysShopId, sysShopId)
                .in(ShopProductSkuEntity::getProductId, productIds)
                .remove();
    }

    @Override
    public List<ShopProductSkuEntity> getShopProductListBySysShopId( String sysShopId ) {
        List<ShopProductSkuEntity> shopProductSkuEntities = super.lambdaQuery()
                .eq(ShopProductSkuEntity::getSysShopId, sysShopId)
                .list();
        return shopProductSkuEntities;
    }

    @Override
    public ShopProductSkuEntity getShopProductBysku( String sysShopId, String sku ) {
        return lambdaQuery()
                .eq(ShopProductSkuEntity::getSysShopId, sysShopId)
                .eq(ShopProductSkuEntity::getSku, sku)
                .one();
    }
}
